package com.quanyan.operative.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.operative.model.req.ReqUserHitEgg;
import com.quanyan.operative.model.req.ReqUserReward;
import com.quanyan.operative.service.DoubleEggFestivalService;
import com.quanyan.operative.service.WxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 双蛋活动
 *
 * Created by huadongyang on 2016/12/7.
 */
@RestController
@RequestMapping(value = "/api/operative/doubleEggFestival", method = RequestMethod.POST, produces = "application/json")
public class DoubleEggFestivalController {
    private static final Logger logger = LoggerFactory.getLogger(DoubleEggFestivalController.class);
    @Autowired
    private DoubleEggFestivalService doubleEggFestivalService;

    @Autowired
    private WxService wxService;

    /**
     * 获取活动和奖品列表
     *
     * @return
     */
    @RequestMapping(value = "/getPrizeAndActivityList")
    APIResponse getPrizeAndActivityList(HttpServletRequest request){
        Integer uid = (Integer) request.getAttribute("uid");
        return doubleEggFestivalService.getPrizeAndActivityList(uid);
    }


    /**
     * 帮好友砸蛋
     *
     * @param reqUserHitEgg
     * @param result
     * @param request
     * @return
     */
    @RequestMapping(value = "/helpFriendHitEgg")
    APIResponse helpFriendHitEgg(@RequestBody @Valid ReqUserHitEgg reqUserHitEgg, BindingResult result, HttpServletRequest request){
        if (result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }

        if (reqUserHitEgg == null || reqUserHitEgg.getUid() <=0
                || StringUtil.isBlankOrNull(reqUserHitEgg.getOpenId())
                || StringUtil.isBlankOrNull(reqUserHitEgg.getAppId())
                || null == reqUserHitEgg.getActivityId()
                || reqUserHitEgg.getActivityId() <= 0) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        //验证OpenId的合法性
        APIResponse apiResponse = wxService.verifyOpenId(reqUserHitEgg.getAppId(),reqUserHitEgg.getOpenId());
        if (apiResponse.isRet()) {
            return doubleEggFestivalService.helpFriendHitEgg(reqUserHitEgg);
        }else {
            return APIResponse.returnFail(100,"微信未授权");
        }
    }

    /**
     * 获取好友砸蛋列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserHitEggList")
    APIResponse getUserHitEggList(@RequestBody Map map, HttpServletRequest request){
        Integer uid = (Integer)map.get("uid"); //用户id
        Integer activityId = (Integer)map.get("activityId");

        logger.info("用户uid:{}", uid);
        if (uid == null || activityId == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return doubleEggFestivalService.getUserHitEggList(uid,activityId);
    }

    /**
     * 用户兑奖
     * @param reqUserReward
     * @param bindingResult
     * @param request
     * @return
     */
    @RequestMapping(value = "/userReward")
    APIResponse userReward(@RequestBody @Valid ReqUserReward reqUserReward, BindingResult bindingResult, HttpServletRequest request){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        if (reqUserReward == null || reqUserReward.getActivityId() == null || reqUserReward.getPrizeId() == null || reqUserReward.getMobile() == null ) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer uid = (Integer)request.getAttribute("uid"); //用户id
        if (uid == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        reqUserReward.setUid(uid);
        logger.info("用户兑奖请求参数reqUserReward：{}",reqUserReward.toString());

        return doubleEggFestivalService.userReward(reqUserReward);
    }

    /**
     * 获取好友砸蛋列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserEggshellNum")
    APIResponse getUserEggshellNum(@RequestBody Map map, HttpServletRequest request){
        Integer uid = (Integer)map.get("uid"); //用户id
        Integer activityId = (Integer)map.get("activityId");

        logger.info("用户uid:{},活动id：{}", uid, activityId);
        if (uid == null || activityId == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return doubleEggFestivalService.getUserEggshellNum(uid,activityId);
    }
}
